package com.panshi.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.panshi.interceptor.GlobalInterceptor;
import com.panshi.tools.ResponseData;

import java.util.ArrayList;
import java.util.List;

public class SymbolController extends Controller {


    /**
     * 获得所有的交易品种
     */
    @ActionKey("/symbols")
    @Before(GlobalInterceptor.class)
    public void getSymbolList() {


        List<Record> symbollist = Db.find("SELECT *  from ot_app_symbol_channel  left JOIN ot_app_symbols on ot_app_symbol_channel.sid=ot_app_symbols.id where type=1;");

        List<Record> symbolListRecord = new ArrayList<Record>();

        for (Record symbol : symbollist) {
            Record data = new Record();
            ////品种ID
            data.set("id", symbol.get("id"));
            ////品种颜色
            data.set("color", symbol.get("color"));
            ////1可用, 0不可用
            data.set("status", symbol.get("status"));
            //// name: "美原油", //品种中文名称
            data.set("name", symbol.get("name"));
            ////symbolName: "CL", //品种英文名称
            data.set("symbolName", symbol.get("symbol_name"));
            // contractName: "CL1705 美国纽约商品交易所17年5月份合约", //合约名称
            data.set("contractName", symbol.get("symbol_name_leverate"));
            //comment: ["都在玩[1200元起]","06:00-次日凌晨05:00"] //品种说明信息
            String[] comment = new String[]{symbol.get("comment"), symbol.get("trade_time")};
            data.set("comment", comment);
            //contractUnit: 1000, //合约单位(美元/手)
            data.set("contractUnit", symbol.get("contract_unit"));
            //overnightFee: 0, //隔夜费
            data.set("overnightFee", symbol.get("Overnightfee"));
            //commission: 0, //手续费
            data.set("commission", symbol.get("commission"));
            // digits: 2, //报价精度
            data.set("digits", symbol.get("digits"));
            //leverage: 200, //杠杆
            data.set("leverage", symbol.get("leverage"));
            //volumes: [0.01, 0.02, 0.1, ...], //可交易手数
            String[] volumes = symbol.get("volumes").toString().split(",");
            data.set("volumes", symbol.get("volumes"));
            // tradingTime: [0: ["06:00-24:00"], 1: ["00:00-5:00", "06:00-24:00"], ...], //交易时间段
            String trading_time_list = symbol.getStr("trading_time_list");
            String[] split = trading_time_list.split(";");
            List<String[]> tradingTime = new ArrayList<String[]>();
            for (int i = 0; i < split.length; i++)
                tradingTime.add(split[i].split(","));
            data.set("tradingTime", tradingTime);
            //addition
            String[] addition = new String[]{symbol.getStr("symbol_addtion_date1"), symbol.getStr("symbol_addtion_date2"), symbol.getStr("symbol_addtion_date3"), symbol.getStr("symbol_addtion_date4"), symbol.getStr("symbol_addtion_date5"), symbol.getStr("symbol_addtion_date6")};
            data.set("addition", addition);
            //additionDemo
            String[] additionDemo = new String[]{symbol.getStr("symbol_demo_addtion_date1"), symbol.getStr("symbol_demo_addtion_date2"), symbol.getStr("symbol_demo_addtion_date3"), symbol.getStr("symbol_demo_addtion_date4"), symbol.getStr("symbol_demo_addtion_date5"), symbol.getStr("symbol_demo_addtion_date6"),};
            data.set("additionDemo", additionDemo);
            symbolListRecord.add(data);
        }
        renderJson(ResponseData.ok().putDataValue("symbolList", symbolListRecord));
    }


    /**
     * 获取交易动态
     */
    @ActionKey("/trading-dynamics")
    @Before(GlobalInterceptor.class)
    public void get_trade_dynamics() {


//        System.out.println("进入到Controller");
//
//        try {
//            //让当前的线程睡眠3秒钟，模拟网络延迟而导致表单重复提交的现象
//            Thread.sleep(5 * 1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        List<Record> trade_dynamics = Db.find("select `type`,nickname,symbol,time  from ot_app_trade_dynamics order by id desc limit 10");
        renderJson(ResponseData.ok().putDataValue("trade_dynamics", trade_dynamics));
    }


}
